package com.sky.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private DishMapper dishMapper;

    /**
     * 新增套餐
     * @param setmealDTO
     */
    @Transactional
    @Override
    public void saveWithSetmealDish(SetmealDTO setmealDTO) {
        //封装类型转换，将dto对象拆分成setmeal和setmeal_dish两个对象
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.save(setmeal);
        Long setmealId = setmeal.getId();
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> {
                setmealDish.setSetmealId(setmealId);
            });
        //取出的setmealDishes并没有setmealID属性所以需要每条都要加上
        //分别将两个对象存储到对应的数据库中,记住新增菜品的时候要使用自动添加公共字段
        //由于对两个表进行操作所以新增套餐是一个事务
        //还要用到文件上传
        setmealDishMapper.saveBatch(setmealDishes);
    }

    /**
     * 套餐分页查询
     * @param setmealPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        //先进行PageHelper分页查询
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        List<SetmealVO> setmealVOList = setmealMapper.pageQuery(setmealPageQueryDTO);
        PageInfo<SetmealVO> pageInfo = new PageInfo<>(setmealVOList);
        return new PageResult(pageInfo.getTotal(),pageInfo.getList());
    }

    /**
     * 根据套餐id删除套餐
     * @param ids
     */
    @Transactional
    @Override
    public void deleteByIds(List<Long> ids) {
        //判断套餐的起售条件
        //1.先通过ids获取套餐列表，然后通过foreach判断每个套餐的起售状况
        List<Setmeal> setmealList = setmealMapper.getByIds(ids);
        setmealList.forEach(setmeal -> {
            if(setmeal.getStatus() == StatusConstant.ENABLE)
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
        });
        //删除套餐的同时也要删除setmeal_dish表中与该套餐相关的信息
        setmealMapper.deleteByIds(ids);
        setmealDishMapper.deleteBySetmealIds(ids);
    }

    /**
     * 更新套餐信息
     * @param setmealDTO
     */
    @Transactional
    @Override
    public void updateWithSetmealDish(SetmealDTO setmealDTO) {
        //更新套餐信息需要更新两个表，其中一个setmeal表中的信息，还有一个setmeal_dish中的对应关系
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        //调用Mapper层时记得填充公共字段
        setmealMapper.update(setmeal);
        //获取setmeal的id,先删除setmeal_dish表中的对应关系再插入
        setmealDishMapper.deleteBySetmealId(setmealDTO.getId());
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> {
            setmealDish.setSetmealId(setmealDTO.getId());
        });
        setmealDishMapper.saveBatch(setmealDishes);
    }

    /**
     * 根据id查找套餐和套餐与菜品的对应关系
     * @param id
     * @return
     */
    @Override
    public SetmealVO getByIdWithDish(Long id) {
        Setmeal setmeal = setmealMapper.getbyId(id);
        List<SetmealDish> setmealDishes = setmealDishMapper.getBySetmealId(id);
        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal,setmealVO);
        setmealVO.setSetmealDishes(setmealDishes);
        return setmealVO;
    }

    /**
     * 套餐的起售和停售
     * @param status
     * @param id
     */
    @Override
    public void startOrStop(Integer status, Long id) {
        //起售套餐时，判断套餐内是否有停售菜品，若是有则无法起售抛出异常
        if(status == StatusConstant.ENABLE){
            List<Dish> dishList = dishMapper.getBySetmealId(id);
            dishList.forEach(dish -> {
                if(dish.getStatus() == StatusConstant.DISABLE){
                    throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ENABLE_FAILED);
                }
            });
        }
        Setmeal setmeal = Setmeal.builder()
                .id(id).status(status).build();
        setmealMapper.update(setmeal);
    }

    /**
     * 根据分类id查找套餐
     * @param categoryId
     * @return
     */
    @Override
    public List<Setmeal> getByCategoryId(Long categoryId) {
        Setmeal setmeal = Setmeal.builder()
                .categoryId(categoryId)
                .status(StatusConstant.ENABLE)
                .build();
        List<Setmeal> setmealList = setmealMapper.getByCategoryId(setmeal);
        return setmealList;
    }

    /**
     * 用户端 根据套餐id查询菜品
     * @param setmealId
     * @return
     */
    @Override
    public List<DishItemVO> getDishItemListBySetmealId(Long setmealId) {
         return setmealMapper.getDishItemListBySetmealId(setmealId);
    }


}
